//
//  PTManagerViewController.swift
//  PropertyClient
//
//  Created by benwang on 2021/6/22.
//

import UIKit
import CollectionKit

class PTManagerViewController: UIViewController {

  var collectionView = CollectionView()
  let locationButtonItem = LocationButtonItem(maxLength: 12)
  let sheetHandler = PTSheetHandle()

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    opacityNavigationBar(color: .designKit.secondaryBackground)
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    navigationItem.leftBarButtonItem = locationButtonItem
    locationButtonItem.text = "正在定位中..."

    view.backgroundColor = .designKit.secondaryBackground
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }

    let header = UnitImageTextView()
    header.image = UIImage(named:ManagerItem.bbfx.icon)
    header.text = ManagerItem.bbfx.rawValue
    header.layout = .leading
    header.font = .designKit.body

    let headerProvider = SimpleViewProvider(
        views: [header],
        sizeStrategy: (.fill, .absolute(56))
    )

    headerProvider.tapHandler = { context in
      self.jump(to: .bbfx)
    }

    let headerTitles = ["缴费", "公告", "报修", "人员"]

    let sections: [Provider] = (0..<headerTitles.count).map { at in
      let dataSource = ArrayDataSource<ManagerItem>(
        data: ManagerItem.items[at]
      )

      let viewSource = ClosureViewSource<ManagerItem, UnitImageTextView> { item, data, index in
        item.image = UIImage(named: data.icon)
        item.text = data.rawValue
        item.font = .designKit.body
      }

      let sizeSource = { (index: Int, data: ManagerItem, size: CGSize) in
        return CGSize(width: (size.width - 26) / 3, height: 56)
      }

      let itemProvider = BasicProvider<ManagerItem, UnitImageTextView>(
        dataSource: dataSource,
        viewSource: viewSource,
        sizeSource: sizeSource,
        layout: FlowLayout(spacing: 13)
      )

      itemProvider.animator = FadeAnimator()
      itemProvider.tapHandler = { (context) in
        self.jump(to: context.data)
      }

      return itemProvider
    }

    let provider = ComposedHeaderProvider(
      layout: FlowLayout(spacing: .extraSmall),
      headerViewSource: { (view: UILabel, data, index) in
          view.textColor = .designKit.primaryText
          view.font = .designKit.caption
          view.text = headerTitles[index]
      },
      headerSizeSource: { (index, data, maxSize) -> CGSize in
        return CGSize(width: maxSize.width, height: .twoExtraLarge)
      },
      sections: sections
    )

    collectionView.provider = ComposedProvider(
      layout: FlowLayout(spacing: 20).inset(by: .padding(.normal)),
      sections: [headerProvider, provider]
    )

    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
      self.locationButtonItem.text = "老北京四合院"
    }
  }

  func jump(to item: ManagerItem) {
    switch item {
    case .bbfx:
      let vc = ReportAnalysisViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)

    case .wyjf, .gnjf, .cwjf:
      let vc = DemandNoteViewController()
      vc.title = item.rawValue
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)

    case .bxsh:
      let vc = RepairOrderViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .bxdd:
      let vc = ReportOrderViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .sfpj:
      let vc = EvaluationStatisticalViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)

    case .fbgg:
      let vc = NoticePublishViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .ggcs:
      let vc = NoticeFirstTrialViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .ggfs:
      let vc = NoticeRetrialViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)

    case .yggl:
      let vc = EmployeeManagementViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .ldgl:
      let vc = BuildingManagementViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .jmgl:
      let vc = ResidentManagementViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    }
  }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

enum ManagerItem: String {
  case bbfx = "报表分析"

  case wyjf = "物业缴费"
  case gnjf = "供暖缴费"
  case cwjf = "车位缴费"

  case fbgg = "发布公告"
  case ggcs = "公告初审"
  case ggfs = "公告复审"

  case bxsh = "报修审核"
  case bxdd = "报修订单"
  case sfpj = "师傅评价"

  case yggl = "员工管理"
  case ldgl = "楼栋管理"
  case jmgl = "居民管理"

  var icon: String {
    switch self {
    case .bbfx:
      return "m_bbfx"
    case .wyjf:
      return "m_wyjf"
    case .gnjf:
      return "m_gnjf"
    case .cwjf:
      return "m_cwjf"
    case .fbgg:
      return "m_fbgg"
    case .ggcs:
      return "m_ggcs"
    case .ggfs:
      return "m_ggfs"

    case .bxsh:
      return "m_bxsh"
    case .bxdd:
      return "m_bxdd"
    case .sfpj:
      return "m_sfpj"

    case .yggl:
      return "m_yggl"
    case .ldgl:
      return "m_ldgl"
    case .jmgl:
      return "m_jmgl"
    }
  }

  static let items: [[ManagerItem]] = [
    [.wyjf, .gnjf, .cwjf],
    [.fbgg, .ggcs, .ggfs ],
    [.bxsh, .bxdd, .sfpj],
    [.yggl, .ldgl, .jmgl],
  ]
}
